define(['jquery', 'bootstrap', 'backend', 'table', 'form',], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {

            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'closeroom/index',
                    add_url: 'closeroom/add',
                  //  edit_url: 'closeroom/edit',
                    del_url: 'closeroom/del',
                    multi_url: 'closeroom/multi',
                    dragsort_url: 'ajax/weigh',
                    table: 'category',
                }
            });

            var table = $("#table");
            var tableOptions = {
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk:'cid',
                sortName:'cid',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'chtitle', title: __('chtitle'), align: 'left',search:true},
                        {field: 'count', title: '关闭房间数量', align: 'left'},
                     /*   {field: 'crcode', title: __('crcode'), align: 'left'},
                        {field: 'crtitle', title: __('crtitle'), align: 'left'},*/
                   /*     {field: 'cstime', title: __('cstime'), align: 'left'},
                        {field: 'cetime', title: __('cetime'), align: 'left'},*/
                        {field: 'ctime', title: __('ctime'), align: 'left'},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons:[
                                {
                                    name: 'searchroom',
                                    text: '查看房间',
                                    title: __('查看房间'),
                                    classname: 'btn btn-xs btn-info btn-view btn-dialog',
                                    extend:'data-area=\'["90%","90%"]\'',
                                    icon: 'fa fa-list',
                                    url: 'closehouse?ref=addtabs',

                                }
                            ]
                        }
                    ]
                ],
                commonSearch: false,

            };
            // 初始化表格
            table.bootstrapTable(tableOptions);

            // 为表格绑定事件
            Table.api.bindevent(table);

            //修改添加是弹窗大小
            $(".btn-add").data("area", ["80%","80%"]); //自定义弹窗大小
            //修改编辑弹窗大小
            table.on('post-body.bs.table',function () {
                $(".btn-editone").data("area", ["80%","80%"]); //自定义弹窗大小
            });

            //绑定TAB事件
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                // var options = table.bootstrapTable(tableOptions);
                var typeStr = $(this).attr("href").replace('#', '');
                var options = table.bootstrapTable('getOptions');
                options.pageNumber = 1;
                options.queryParams = function (params) {
                    // params.filter = JSON.stringify({type: typeStr});
                    params.type = typeStr;

                    return params;
                };
                table.bootstrapTable('refresh', {});
                return false;

            });

            //必须默认触发shown.bs.tab事件
            // $('ul.nav-tabs li.active a[data-toggle="tab"]').trigger("shown.bs.tab");

        },

        add: function () {

            setTimeout(function () {
                $("#c-type").trigger("change");
            }, 100);

            var hotelsid = 0;//默认
            //重构
            $("#hotels").change(function () {

                var sel = $(this).val();
                if(sel == 0){
                    $("#rooms").css("display",'none');
                    $("#rooms2").empty();
                }else{
                   $("#rooms").css("display",'block');
                    $("#rooms2").empty();

                    $.ajax({
                        type: "GET",
                        url: "/api/task/getRooms",
                        data: {hid:sel},
                        dataType: "json",
                        success: function(data){
                            var len = data.length;
                            var html = "";
                            if(len > 0){
                                for(var i=0;i<len;i++){
                                    html += ' <option  value="'+ data[i]['id'] +'">'+ data[i]['h_title']+' '+ data[i]['h_housebrand'] +'</option>';
                                }
                            }
                            $("#rooms2").append(html);
                            $('#rooms2').selectpicker('refresh');
                        }
                    });
                }

            })

            Controller.api.bindevent();
        },
        edit: function () {

            //选中酒店
            var sel = $("#hotels").attr("ids");
            $("#hotels option[value="+ sel +"]").prop("selected",true);
            //获取对应酒店下房间
            var sel2 = $("#rooms").attr("ids");
            $.ajax({
                type: "GET",
                url: "/api/task/getRooms",
                data: {hid:sel2},
                dataType: "json",
                success: function(data){
                    var len = data.length;
                    var html = "";
                    if(len > 0){
                        for(var i=0;i<len;i++){
                            html += ' <option value="'+ data[i]['id'] +'">'+ data[i]['h_title']+' '+ data[i]['h_housebrand'] +'</option>';
                        }
                    }
                    $("#rooms2").append(html);
                    //选中房间
                    var rids = $("#rooms2").attr("ids");
                    var ridsarr= new Array(); //定义一数组
                    ridsarr = rids.split(",");
                    for (i=0;i<ridsarr.length ;i++ )
                    {
                        $("#rooms2 option[value="+ ridsarr[i] +"]").prop("selected",true);
                    }

                    $('#rooms2').selectpicker('refresh');
                }
            });

            //重构
            $("#hotels").change(function () {
                var sel = $(this).val();
                $("#rooms").css("display",'block');
                $("#rooms2").empty();

                $.ajax({
                    type: "GET",
                    url: "/api/task/getRooms",
                    data: {hid:sel},
                    dataType: "json",
                    success: function(data){
                        var len = data.length;
                        var html = "";
                        if(len > 0){
                            for(var i=0;i<len;i++){
                                html += ' <option  value="'+ data[i]['id'] +'">'+ data[i]['h_title']+' '+ data[i]['h_housebrand'] +'</option>';
                            }
                        }
                        $("#rooms2").append(html);
                        $('#rooms2').selectpicker('refresh');
                    }
                });
            })

            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                $(document).on("change", "#c-type", function () {
                    $("#c-pid option[data-type='all']").prop("selected", true);
                    $("#c-pid option").removeClass("hide");
                    $("#c-pid option[data-type!='" + $(this).val() + "'][data-type!='all']").addClass("hide");
                    $("#c-pid").data("selectpicker") && $("#c-pid").selectpicker("refresh");
                });
                Form.api.bindevent($("form[role=form]"));
            },

        }
    };
    return Controller;
});
